var sid;
var pid;

var sname;
var kind;



function searchStaff(){//$("#testform").serialize() 
		$.post("getStaff.do", "name="+$("#staffName").val(),function(data){
			var list = document.getElementsByClassName("staffmsglist");
			var p = document.getElementById("tabledata");
			for(var k=list.length-1;k>=0;k--) p.removeChild(list[k]);
			var res = data.split("\u0001");
				for(var i=0;i<res.length-1;i++){
						var caozuo = document.getElementById("moban").cloneNode(true);
						var tr = document.createElement("tr");
						tr.setAttribute("class","staffmsglist choose");
						var str = res[i].split("\u0005");
						for(var j=0;j<str.length;j++){
							var td = document.createElement("td");
							td.innerHTML = str[j];
							tr.appendChild(td);
						}
					tr.setAttribute("dataofid",str[0]);
					tr.setAttribute("sname",str[1]);
					tr.setAttribute("onclick","seaarchPassPort(this)");
					tr.appendChild(caozuo);
					p.appendChild(tr);
			}
		},"text");

}

function changeColor(node){
	var ch = document.getElementsByClassName("choose");
	for(var i=0;i<ch.length;i++) ch[i].style.backgroundColor = "";
	node.style.backgroundColor = "#66CC66";
}

function seaarchPassPort(node){
	changeColor(node);
	if(node!=-1){
		sid = node.getAttribute("dataofid");
		sname = node.getAttribute("sname");
		document.getElementById("sssname").value = sname;
	}
	var p = document.getElementById("idonkonw");
	var child = document.getElementById("myModalLabel333");
	p.removeChild(child);
	var m = document.getElementById("myModalLabel222").cloneNode(true);
	m.setAttribute("id","myModalLabel333");
	m.style.display = "inline";
	p.appendChild(m);
	$.post("getPassPort.do", "sid="+sid,function(data){
		var res = data.split("\u0001");
		for(var i=0;i<res.length-1;i++){
			var str = res[i].split("\u0005");
			var n = document.createElement("button");
			n.setAttribute("type","button");
			n.setAttribute("class","btn btn-default passport");
			n.setAttribute("data-toggle","modal");
			n.setAttribute("data-target","#myModal4");
			n.setAttribute("pid",str[0]);
			n.setAttribute("kind",str[2]);
			n.setAttribute("onclick","setPid(this)");
			n.innerHTML = str[2];
			document.getElementById("myModalLabel333").appendChild(n);
		}
		
	},"text");
}

function setPid(node){
	pid = node.getAttribute("pid");
	kind = node.getAttribute("kind");
	
	document.getElementById("addActionm").value = "姓名："+sname+"    证件："+kind;
}

function addAction(){
	var country = $("#country").text();
	var college = $("#college").text();
	var outdate = $("#outdate").text();
	var subject = $("#subject").text();
	var costby = $("#costby").text();
	var backdate = $("#backdate").text();
	var outtype = getSelect($("#outtype"));
	var remake = $("#remake").text();
	var job = getSelect($("#job"));
	var adl = getSelect($("#adl"));
	var post = getSelect($("#post"));
	
	if(!datematch(outdate)) return;
	if(backdate!="" && !datematch(backdate)) return;
	
	if(country.length<1 || college.length<1 || outdate.length<1 || subject.length<1 || costby.length<1 || 
			subject.length<1 ){
			alert("所有空都不许为空！请完整填写");
			return;
		}
	var parm = "country="+country+"&college="+college+"&outdate="+outdate+"&subject="+subject;
	parm+="&costby="+costby+"&backdate="+backdate+"&outtype="+outtype+"&remake="+remake+"&id="+pid;
	parm+="&job="+job+"&adl="+adl+"&post="+post;
	$.post("addAction.do", parm,function(data){
		alert("添加成功！");
	},"text");
}

function addStaff(){
	var name = $("#name").text();
	var personid = $("#personid").text();
	var sex = getSelect($("#sex"));
	var nation = getSelect($("#nation"));
	var face = getSelect($("#face"));
	var department = getSelect($("#department"));
	var education = getSelect($("#education"));
	var contact = $("#contact").text();
	
	if(name.length<1 || personid.length<1 || sex.length<1 || nation.length<1 || face.length<1 ||
		 department.length<1 || education.length<1 || contact.length<1){
			alert("所有空都不许为空！请完整填写");
			return;
		}
	var parm = "xxx=hhh&name="+name+"&personid="+personid+"&sex="+sex+"&nation="+nation+"&face="+face;
	parm+="&department="+department+"&education="+education+"&contact="+contact;
	
	
	$.post("addStaff.do", parm,function(data){
		alert("添加成功！");
		searchStaff();
	},"text");
}

function addPassPort(){
	var id = $("#id").text();
	var kind = getSelect($("#kind"));
	var startdate = $("#startdate").text();
	var enddate = $("#enddate").text();
	var returndate = $("#returndate").text();
	if(!datematch(startdate)) return;
	if(!datematch(enddate)) return;
	if(id.length<1 || kind.length<1 || startdate.length<1 || enddate.length<1){
		alert("所有空都不许为空！请完整填写");
			return;
	}
	
	var parm = "id="+id+"&kind="+kind+"&startdate="+startdate+"&enddate="+enddate+"&sid="+sid+"&returndate="+returndate;
	$.post("addPassPort.do", parm,function(data){
		var rongqi = document.getElementById("myModalLabel333");
		var n = document.createElement("button");
		n.setAttribute("type","button");
		n.setAttribute("class","btn btn-default passport");
		n.setAttribute("data-toggle","modal");
		n.setAttribute("data-target","#myModal4");
		n.setAttribute("pid",id);
		n.innerHTML = kind;
		n.setAttribute("kind",kind);
		n.setAttribute("onclick","setPid(this)");
		rongqi.appendChild(n);
		alert("添加成功！");
		document.getElementById("canceled").click();
		
	},"text");
}

function getSelect(myselect){
	var index=myselect[0].selectedIndex;
	return myselect[0].options[index].text;
}

function datematch(date){
	var reg = /^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/;
	var regExp = new RegExp(reg);
	if(!regExp.test(date)){
		alert("日期格式不正确！正确格式为：2015-01-01");
		return false;
	}
	else return true;          
}

function chooseDepartment(index){
	var ch = document.getElementsByClassName("chooseDepartment");
	for(var i=0;i<ch.length;i++){
		ch[i].style.display = "none";
		ch[i].setAttribute("id","");
	}
	ch[index].setAttribute("id","department");
	ch[index].style.display = "inline";
}
